package com.cheney.weiboo.service;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;

import com.cheney.weiboo.db.DBHelper;
import com.cheney.weiboo.domain.Users;
import com.cheney.weiboo.utils.DBFactory;

/**
 * 用于处理用户的相关数据
 * @author mayh
 *
 */
public class UserService {

	private DBHelper dbHelper = null;
	private String DB_NAME = "weiboo.db";
	private SQLiteDatabase db = null;
	private String tableName = "users";
	public UserService(Context context){
		dbHelper = DBFactory.getInstance(context, DB_NAME);
		db = dbHelper.getWritableDatabase();
		String sql = "CREATE TABLE IF NOT EXISTS USERS ("
			 +Users.ID+" varchar primary key,"
			 +Users.TOKEN+" varchar,"
			 +Users.TOKENSECRET+" varchar,"
			 +Users.USERID+" varchar,"
			 +Users.USERNAME+" varchar,"
			 +Users.USERICON+" blob"
			 +")" ;
		db.execSQL(sql);
	}
	
	/**
	 * 判断是否存在某条记录
	 * @param userId
	 * @return
	 */
	public  boolean isExits(String userId){
		boolean flag = false;
		Cursor cursor = db.query(tableName, null, Users.ID+"=?", new String[]{userId}, null, null, null);
		flag = cursor.moveToFirst();
		cursor.close();
		return flag;
	}
	
	/**
	 * 保存用户
	 * @param user
	 */
	public long saveUsers(Users user,byte[] icon){
		ContentValues cv = new ContentValues();
		cv.put(Users.ID, user.getId());
		cv.put(Users.USERNAME, user.getUserName());
		cv.put(Users.USERID, user.getUserId());
		cv.put(Users.TOKEN, user.getToken());
		cv.put(Users.TOKENSECRET, user.getTokenSecret());
		if(icon!=null){
			cv.put(Users.USERICON, icon);
		}
		return db.insert(tableName, null, cv);
	}
	
	/**
	 * 获得用户列表
	 * @param simple
	 * @return
	 */
	public List<Users> getUserList(boolean simple){
		List<Users> usersList = new ArrayList<Users>();
		Cursor cursor = db.query(tableName, null, null, null, null, null, Users.ID+" DESC");
		cursor.moveToFirst();
		System.out.println(cursor.getCount());
		while(!cursor.isAfterLast()){
			Users users = new Users();
			users.setId(cursor.getString(0));
			users.setToken(cursor.getString(1));
			users.setTokenSecret(cursor.getString(2));
			users.setUserId(cursor.getString(3));
			if(!simple){
				users.setUserName(cursor.getString(4));
				if(cursor.getBlob(5)!=null){
				ByteArrayInputStream bis = new ByteArrayInputStream(cursor.getBlob(5));
				Drawable drawable = Drawable.createFromStream(bis, "image");
				users.setUserIcon(drawable);
				}else {
//					users.setUserIcon(R.drawable.icon);
				}
			}
			usersList.add(users);
			cursor.moveToNext();
		}
		cursor.close();
		return usersList;
	}
	//根据名字查询
	public  Users getUserByName(String userName,List<Users> userList){
		Users userInfo = null;
		int size = userList.size();
		for(int i=0;i<size;i++){
			if(userName.equals(userList.get(i).getUserName())){
				userInfo = userList.get(i);
				break;
			}
		}
		return userInfo;
	}
	//删除
	public void deleteById(String id){
		db.delete(tableName, Users.ID+"=?", new String[]{id});
	}
	//更新
	public void update(String id,String userName,Bitmap icon){
		ContentValues cv = new ContentValues();
		cv.put(Users.USERNAME,userName);
		final ByteArrayOutputStream os = new ByteArrayOutputStream();
		icon.compress(Bitmap.CompressFormat.PNG, 100, os);
		cv.put(Users.USERICON, os.toByteArray());
		db.update(tableName, cv, Users.ID+"=?", new String[]{id});
	}
}
